/*
 * Copyright (C) 2016  Nicola Spanti (RyDroid) <dev@nicola-spanti.info>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */


#include <check.h>
#include "PlanetWars2dRT-core/utils/vector_generic.h"


DEFINE_VECTOR_GENERIC_STATIC(int)
struct vector_int v1, v2;


START_TEST(test_vector_generic_init_first_time_only)
{
  VECTOR_GENERIC_STATIC_INIT_FIRST_TIME_ONLY(&v1, 0, int);
  ck_assert(VECTOR_GENERIC_STATIC_IS_EMPTY_UNSAFE(&v1));
  ck_assert_uint_eq(VECTOR_GENERIC_STATIC_GET_SIZE_UNSAFE(&v1), 0);
  ck_assert_uint_le(VECTOR_GENERIC_STATIC_GET_SIZE_UNSAFE(&v1),
		    VECTOR_GENERIC_STATIC_GET_SIZE_ALLOCATED_UNSAFE(&v1));
  VECTOR_GENERIC_STATIC_DESTRUCTOR(&v1);
  
  VECTOR_GENERIC_STATIC_INIT_FIRST_TIME_ONLY(&v2, 5, int);
  ck_assert(VECTOR_GENERIC_STATIC_IS_EMPTY_UNSAFE(&v2));
  ck_assert_uint_eq(VECTOR_GENERIC_STATIC_GET_SIZE_UNSAFE(&v2), 0);
  ck_assert_uint_eq(VECTOR_GENERIC_STATIC_GET_SIZE_ALLOCATED_UNSAFE(&v2), 5);
  VECTOR_GENERIC_STATIC_DESTRUCTOR(&v2);
}
END_TEST

START_TEST(test_vector_generic_init_first_time_only_null)
{
  VECTOR_GENERIC_STATIC_INIT_FIRST_TIME_ONLY_NULL(&v1, 2, int);
  
  ck_assert_uint_eq(VECTOR_GENERIC_STATIC_GET_SIZE_UNSAFE(&v1), 2);
  ck_assert_int_eq(VECTOR_GENERIC_STATIC_GET_ITEM_VALUE_UNSAFE(&v1, 0), 0);
  ck_assert_int_eq(VECTOR_GENERIC_STATIC_GET_ITEM_VALUE_UNSAFE(&v1, 1), 0);
  
  VECTOR_GENERIC_STATIC_DESTRUCTOR(&v1);
}
END_TEST

START_TEST(test_vector_generic_iterator)
{
  VECTOR_GENERIC_STATIC_INIT_FIRST_TIME_ONLY_NULL(&v1, 2, int);
  
  for(int* it = VECTOR_GENERIC_STATIC_GET_BEGIN_POINTER(&v1);
      it != VECTOR_GENERIC_STATIC_GET_END_POINTER(&v1);
      ++it)
    {
      ck_assert_int_eq(*it, 0);
    }
  
  VECTOR_GENERIC_STATIC_DESTRUCTOR(&v1);
}
END_TEST

START_TEST(test_vector_generic_clear)
{
  VECTOR_GENERIC_STATIC_INIT_FIRST_TIME_ONLY_NULL(&v1, 2, int);
  ck_assert_uint_eq(VECTOR_GENERIC_STATIC_GET_SIZE_UNSAFE(&v1), 2);
  
  VECTOR_GENERIC_STATIC_CLEAR(&v1);
  ck_assert(VECTOR_GENERIC_STATIC_IS_EMPTY_UNSAFE(&v1));
  ck_assert_uint_eq(VECTOR_GENERIC_STATIC_GET_SIZE_UNSAFE(&v1), 0);
  ck_assert_uint_eq(VECTOR_GENERIC_STATIC_GET_SIZE_ALLOCATED_UNSAFE(&v1), 0);
  ck_assert_ptr_eq(VECTOR_GENERIC_STATIC_GET_DATA_UNSAFE(&v1), NULL);
  
  VECTOR_GENERIC_STATIC_DESTRUCTOR(&v1);
}
END_TEST

START_TEST(test_vector_generic_push_back)
{
  VECTOR_GENERIC_STATIC_INIT_FIRST_TIME_ONLY(&v1, 0, int);
  ck_assert_uint_eq(VECTOR_GENERIC_STATIC_GET_SIZE_UNSAFE(&v1), 0);
  
  VECTOR_GENERIC_STATIC_PUSH_BACK(&v1, 0, int);
  ck_assert_int_eq(VECTOR_GENERIC_STATIC_GET_ITEM_VALUE_UNSAFE(&v1, 0), 0);
  ck_assert_uint_eq(VECTOR_GENERIC_STATIC_GET_SIZE_UNSAFE(&v1), 1);
  
  VECTOR_GENERIC_STATIC_PUSH_BACK(&v1, 1, int);
  ck_assert_int_eq(VECTOR_GENERIC_STATIC_GET_ITEM_VALUE_UNSAFE(&v1, 1), 1);
  ck_assert_uint_eq(VECTOR_GENERIC_STATIC_GET_SIZE_UNSAFE(&v1), 2);
  
  VECTOR_GENERIC_STATIC_PUSH_BACK(&v1, 10, int);
  ck_assert_int_eq(VECTOR_GENERIC_STATIC_GET_ITEM_VALUE_UNSAFE(&v1, 2), 10);
  ck_assert_uint_eq(VECTOR_GENERIC_STATIC_GET_SIZE_UNSAFE(&v1), 3);
  
  VECTOR_GENERIC_STATIC_DESTRUCTOR(&v1);
}
END_TEST

START_TEST(test_vector_generic_pop_back)
{
  VECTOR_GENERIC_STATIC_INIT_FIRST_TIME_ONLY_NULL(&v1, 2, int);
  ck_assert_uint_eq(VECTOR_GENERIC_STATIC_GET_SIZE_UNSAFE(&v1), 2);
  
  VECTOR_GENERIC_STATIC_POP_BACK(&v1);
  ck_assert_uint_eq(VECTOR_GENERIC_STATIC_GET_SIZE_UNSAFE(&v1), 1);
  
  VECTOR_GENERIC_STATIC_POP_BACK(&v1);
  ck_assert_uint_eq(VECTOR_GENERIC_STATIC_GET_SIZE_UNSAFE(&v1), 0);
  
  VECTOR_GENERIC_STATIC_DESTRUCTOR(&v1);
}
END_TEST
	

Suite*
vector_suite(void)
{
  Suite* s = suite_create("Vector");
  
  TCase* tc_vector_generic_init = tcase_create("Init");
  tcase_add_test(tc_vector_generic_init,
		 test_vector_generic_init_first_time_only);
  tcase_add_test(tc_vector_generic_init,
		 test_vector_generic_init_first_time_only_null);
  suite_add_tcase(s, tc_vector_generic_init);
  
  TCase* tc_vector_generic_access = tcase_create("Element access");
  tcase_add_test(tc_vector_generic_access,
		 test_vector_generic_iterator);
  suite_add_tcase(s, tc_vector_generic_access);
  
  TCase* tc_vector_generic_modifiers = tcase_create("Modifiers");
  tcase_add_test(tc_vector_generic_modifiers,
		 test_vector_generic_clear);
  tcase_add_test(tc_vector_generic_modifiers,
		 test_vector_generic_push_back);
  tcase_add_test(tc_vector_generic_modifiers,
		 test_vector_generic_pop_back);
  suite_add_tcase(s, tc_vector_generic_modifiers);
  
  return s;
}

int
main(void)
{
  size_t number_failed;
  Suite* s;
  SRunner* sr;
  
  s = vector_suite();
  sr = srunner_create(s);
  
  srunner_run_all(sr, CK_NORMAL);
  number_failed = srunner_ntests_failed(sr);
  srunner_free(sr);
  
  return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}
